<?php 
session_start();
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

include 'administrator/components/com_bookman/includes/gzip.php';

include 'administrator/components/com_bookman/includes/toolbar.php';
include 'administrator/components/com_bookman/includes/config.php';
include ADMIN_COMPONENT_PATH.COMPONENT_PATH.'includes/functions.php';
include ADMIN_COMPONENT_PATH.COMPONENT_PATH.'includes/database.php';
include ADMIN_COMPONENT_PATH.COMPONENT_PATH.'includes/orm.php';
include COMPONENT_PATH.'includes/user_models.php';
new Db();

include_once COMPONENT_PATH.'main_controller.php';
start_component();
switch ($_GET['user']){
	case "owner":
		if( !empty($my->id) ){
			$manager = identify_owner();
			$unit_ids = get_unit_ids($manager);
			$page = $_GET['page'];
			
			if( empty($page) ){
				if( count($unit_ids) > 1 ) $page = "home";
				else if( count($unit_ids) == 1 ) {
					$page = "property_details";
					$_GET['uid'] = $unit_ids[0];
				}
				else if( count($unit_ids) == 0 ){
					$page = "new";
				}
			}
			if( ($page == "property_details" || $page == "listing" || $page == "images" || $page == "amenities" || $page == "rates" || $page == "calender") ){
				if( empty($_GET['uid']) || !in_array($_GET['uid'], $unit_ids) ) $page = "home";
			}
			/*if( ($page != "account") && empty($_GET['uid']) ){
				$page = "home";
			}
			else if( !in_array($_GET['uid'], $unit_ids) ) $page = "home";
			echo $page;*/
			include_once COMPONENT_PATH.'owner_page_controller.php';
			show_menu();
			switch($page){
				case "home":
					if($_POST){
						switch ($_POST['form']){
							case "new_unit":
								save_new_unit();
								break;
						}
					}
					show_notice();
					display_home();
					break;
				case "account":
					if($_POST){
						switch ($_POST['form']){
							case "save_mailing_addr":
								save_mailing_addr();
								break;
						}
					}
					show_notice();
					display_account();
					break;
				case "new":
					show_new_unit_page();
					break;
				case "property_details":
					if($_POST){
						switch ($_POST['form']){
							case "save_property_details":
								save_property_details();
								break;
							default:
								display_property_details();
								break;
						}
					}
					show_notice();
					display_property_details();
					break;
				case "listing":
					if($_POST){
						switch ($_POST['form']){
							case "save_listing":
								save_listing();
								break;
							default:
								display_listing();
								break;
						}
					}
					show_notice();
					display_listing();
					break;
				case "images":
					if($_POST){
						switch ($_POST['form']){
							case "upload_image":
								image_uploader();
								break;
							case "move_image":
								move_image();
								break;
							case "delete_image":
								delete_image();
								break;
						}
					}
					show_notice();
					display_images();
					break;
				case "amenities":
					if($_POST){
						save_amenities();
					}
					show_notice();
					display_amenities();
					break;
				case "rates":
					if($_POST){
						switch ($_POST['form']){
							case "save_rates":
								save_rates();
								break;
							case "delete_range":
								delete_price_range();
								break;
						}
					}
					show_notice();
					display_rates();
					break;
				case "calender":
					if($_POST){
						switch ($_POST['form']){
							case "save_blocked_dates":
								save_blocked_dates();
								break;
							default:
								display_calender();
								break;
						}
					}
					show_notice();
					display_calender();
					break;
			}
		}
		else {
			echo "You do not have the authority to view this page. Please login to enter this page";
		}
		break;
	default:
		if( !empty($my->id) ){
			include_once COMPONENT_PATH.'owner_page_controller.php';
			show_menu();
		}
		if($_GET['PropertyId']){
			if( $_POST ) {
			   if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
			   		send_enquiry_form();
					unset($_SESSION['security_code']);
			   } else {
			   		$notice['error'] = 1;
					$notice['msg'] = 'You have provided an invalid security code';
			   }
			}
			show_notice();
			get_unit_details();
		}
		else {
			get_unit_list();
		}
		break;
}
end_component();
?>